﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dhSystemBLL.orderData;
using dhDataAcess.orderLinqDAL;
using baseSet.importExport;
using dhsystem.Main;

namespace dhsystem.orderForm
{
    public partial class FrmProductType : Form
    {
        productTypeBLL productType = new productTypeBLL();
        productTypeModel productTypeRow = new productTypeModel();
        public FrmProductType()
        {
            InitializeComponent();
        }
        //新增按钮，显示新增数据组件
        private void toolAdd_Click(object sender, EventArgs e)
        {
            checkAdd.Visible = true;
            textPtype.Focus();
            if (lblState.Text == "编辑")
            {
                MessageBox.Show("当前在编辑数据状态，请先保存编辑数据或者取消再进行编辑");
                return;
            }
            lblState.Text = "新增";
            if (groupAdd.Visible == false)
            {
                groupAdd.Visible = true;
                groupList.Width = groupList.Width - 288;
                groupAdd.Location = new Point(groupList.Width + 20, groupList.Top);
            }
        }
        //取消按钮，隐藏新增数据组件
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            groupList.Width = 288 + groupList.Width;
            groupAdd.Visible = false;
            lblState.Text  = "正常";
        }
        //打开窗口时显示列表数据并且隐藏新增数据组件
        private void FrmProductType_Load(object sender, EventArgs e)
        {
             bindingSourceProductType.DataSource = productType.getproductTypeAll();
             dGridProductType.DataSource = bindingSourceProductType;
             groupList.Width = 288 + groupList.Width;
             groupAdd.Visible = false;
             DcolumnSet.colSet(moduleInfo.productType , dGridProductType );
             toolStripStatusLabel1.Text = "数据总条数:" + dGridProductType.RowCount.ToString() + "条";
        }
        //保存按钮，插入或修改数据
        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (lblState.Text == "新增")
            {
                if (textPtype .Text  == "")
                {
                    MessageBox.Show("类别名称不能为空");
                }
                else
                {
                    try
                    {
                        DialogResult result = MessageBox.Show("是否新增新的产品类型！", "提示信息", MessageBoxButtons.YesNo);
                        if (result == DialogResult.Yes)
                        {
                            productTypeRow.ptype = textPtype.Text;
                            productTypeRow.ptypeMark = textptypeMark.Text;
                            productTypeRow.ptypeParent = textPtypeParent.Text;
                            productTypeRow.createDate = DateTime.Now;
                            productTypeRow.createUser = textcreateuser.Text;
                            productTypeRow.updateDate = DateTime.Now;
                            productTypeRow.updateUser = textupdateuser.Text;
                            productTypeRow.mark = textmark.Text;
                            productType.productTypeInsert(productTypeRow);
                            bindingSourceProductType.DataSource = productType.getproductTypeAll();
                            if (checkAdd.Checked == true)
                            {
                                lblState.Text = "新增";
                            }
                            else
                            {
                                lblState.Text = "正常";
                            }
                            return;
                        }
                    }
                    catch (FormatException ex)
                    {
                        MessageBox.Show(ex.Message.ToString());
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message.ToString());
                    }
                }
            }
            if (lblState.Text == "编辑")
            {
                try
                {
                    DialogResult result = MessageBox.Show("是否更改产品类型！", "提示信息", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        productTypeRow.ptype = textPtype.Text;
                        productTypeRow.ptypeMark = textptypeMark.Text;
                        productTypeRow.ptypeParent = textPtypeParent.Text;
                        productTypeRow.createDate = DateTime.Now;
                        productTypeRow.createUser = textcreateuser.Text;
                        productTypeRow.updateDate = DateTime.Now;
                        productTypeRow.updateUser = textupdateuser.Text;
                        productTypeRow.mark = textmark.Text;
                        productType.productTypeUpdate(productTypeRow);
                        bindingSourceProductType.DataSource = productType.getproductTypeAll();
                        lblState.Text = "正常";
                        return;
                    }
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
        }
        //工具栏的编辑按钮,修改数据
        private void toolEdit_Click(object sender, EventArgs e)
        {
            checkAdd.Visible = false;
            try
            {
                if (lblState.Text == "新增")
                {
                    MessageBox.Show("当前在新增数据状态，请先保存新增数据或者取消再进行编辑");
                    return;
                }

                lblState.Text = "编辑";

                if (groupAdd.Visible == false)
                {
                    groupAdd.Visible = true;
                    groupList.Width = groupList.Width - 288;
                    groupAdd.Location = new Point(groupList.Width + 20, groupList.Top);
                }

                DataGridViewRow currentRow = dGridProductType.CurrentRow;
                textPtype.Text = currentRow.Cells["ptype"].Value.ToString();
                textPtypeParent.Text = currentRow.Cells["ptypeParent"].Value.ToString();
                textptypeMark.Text = currentRow.Cells["ptypeMark"].Value.ToString();
                textcreateuser.Text = currentRow.Cells["createUser"].Value.ToString();
                textupdateuser.Text = currentRow.Cells["updateUser"].Value.ToString();
                textmark.Text = currentRow.Cells["mark"].Value.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        //删除按钮，删除选中行数据
        private void toolDel_Click(object sender, EventArgs e)
        {
            string ptype = dGridProductType.Rows[dGridProductType.CurrentRow.Index].Cells[1].Value.ToString();
            try
            {
                DialogResult result = MessageBox.Show("是否删除权限组编号为：" + ptype + "的权限组信息！", "提示信息", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {

                    bindingSourceProductType.DataSource = productType.productTypeDel(ptype);
                    dGridProductType.DataSource = bindingSourceProductType;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            bindingSourceProductType.DataSource = productType.getproductTypeAll();
        }
        //工具栏查询按钮，查询所有列表数据
        private void toolQuery_Click(object sender, EventArgs e)
        {
            bindingSourceProductType.DataSource = productType.getproductTypeAll();
            dGridProductType.DataSource = bindingSourceProductType;
        }
        //查询按钮，按条件查询数据
        private void buttonQuery_Click(object sender, EventArgs e)
        {
            try
            {
                bindingSourceProductType .DataSource =productType.getproductTypeAll("%"+textPtypeQuery.Text+"%");
                dGridProductType.DataSource = bindingSourceProductType;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        //工具栏退出按钮，退出窗口
        private void toolExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmProductType_Resize(object sender, EventArgs e)
        {
            if (groupAdd.Visible == true)
            {
                groupAdd.Location = new Point(groupList.Width + 20, groupList.Top);
            }
        }
        //datagridview第一列显示行号
        private void dGridProductType_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            e.Row.HeaderCell.Value = string.Format("{0}", e.Row.Index + 1); 
        }

        private void toolQuery_Click_1(object sender, EventArgs e)
        {
            FrmDataQuery query = new FrmDataQuery(moduleInfo.productType );
            DialogResult result = query.ShowDialog();
            if (result == DialogResult.Yes)
            {
                dGridProductType.DataSource = query.rTable;
            }
        }

        private void toolExport_Click(object sender, EventArgs e)
        {
            FrmExcelExport frm = new FrmExcelExport(moduleInfo.productType , dGridProductType , "产品类型信息");
            frm.ShowDialog();
        }

      
    }
}
